<?php
namespace app\common\model;

use think\Model;
use think\Db;
class DbRule extends Model
{
    // 指定表名,不含前缀
    protected $name = 'db_rule';

    /**
     * 根据分题库id查询出分类id
     * @param $rid
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function getRuleTypeByFdbids($rid){
        if (!$rid)return false;
        $condition['id'] = ['in',$rid];
        $rules = Db::name('db_rule')->where($condition)->field('typeid')->select();//根据分题库id查询出规则
        if ($rules){
            return $rules;
        }else{
            return false;
        }
    }

    public function getListById($rid){
        if (!isset($rid))return false;
        $rules = Db::name('db_rule')->where('id='.$rid)->select();//根据规则id查询出规则
        return $rules;
    }

    /**
     * 根据分题库id 获取想要的字段
     * @param $fdbid
     */
    public function getListByFdbid($fdbid,$field = '*'){
        if (!isset($fdbid))return false;
        $rules = Db::name('db_rule')->where('fdbid='.$fdbid)->field($field)->select();//根据分题库id查询出规则
        return $rules;
    }

    /**
     * 根据分题库规则表id 获取试题
     * @param $rules
     * @param $condition
     * @return array
     */
    public function setDbQuestionsByrule($rules){
        $abs_array = array();//分组用的
        $abstracts = array();//最终生成的数组
        $List = queryQuestionByparam($rules[0]);
//        $page = $List->render();分页
        return ['list' =>$List/*,'page'=>$page*/];

    }

    /**
     * 根据规则id 修改规则表 试题id集合
     * @param $ids 试题ids
     * @param $id  规则id
     */
    public function setQuestionIds($ids,$id){
        if (is_array($ids) && isset($ids)){
            $ids = implode(',',$ids);
        }
        $data['question_ids']=','.$ids;
        $arr = $this->where('id', $id)->update($data);
        return $arr;
    }
}
